#!/usr/bin/env python
# -*- coding: utf-8 -*-

from fabric.api import *
from fabric.colors import red, green
from fabric.contrib.files import *
from fabric.context_managers import *

env.parallel  = True
env.pool_size = 30
env.hosts     = ['192.168.56.104', '192.168.56.106']
env.password  = "111111"

env.roledefs = {
    'local': ['127.0.0.1']
}

# 自定义变量
JAVA_HOME    = '/usr/local/jdk'
WEB_APP_HOME = '/usr/local/apache-tomcat-8.0.28/webapps'


def _hosts():
    """
    自定义目标主机
    """
    with quiet():
        target_machines = local('''cat hosts''', capture=True)
    target_list = target_machines.splitlines()
    return target_list


def chk_date():
    """
    检查集群环境的时间
    """
    run('date +"%Y-%m-%d %H-%M-%S"')


def host_type():
    run('uname -s')

    
def poweroff():
    run('poweroff')
    

def start_tomcat():
    run('/usr/local/apache-tomcat-8.0.28/bin/startup.sh', pty=False)


def stop_tomcat():
    run('kill -9 `pgrep java`')


def restart_tomcat():
    run('kill -9 `pgrep java`')
    run('/usr/local/apache-tomcat-8.0.28/bin/startup.sh', pty=False)
        

def something():
    host_list = _hosts()
    print green('%s' % host_list)

    
def deploy():
    with lcd('/opt/lavenliu'):
        local('svn update')
        with lcd('trunk/fundParent'):
            local('mvn install package -Dmaven.test.skip=true')
            local('cp fund-trade-web/target/fund-trade-web.war WEB_APP_HOME && cp fund-trade-admin/target/fund-trade-admin.war WEB_APP_HOME')


def local01():
    """
    一个测试的函数
    """
    with lcd('/root'):
        local('ls')
        with lcd('python'):
            local('ls')
            local('cp test.py test.py.bak')

    
